{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q trino"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trino.dbapi import connect\n",
    "from trino.auth import OAuth2Authentication\n",
    "from redirect_handler import REDIRECT_HANDLER\n",
    "import urllib3\n",
    "import pandas as pd\n",
    "\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "\n",
    "TRINO_URI = \"https://trino-proxy:443\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use the Catalog (User 1: Peter)\n",
    "Now that the catalog is created, we can use it as usual. Lets login again (if asked) as `peter`, this time directly to the catalog so that we don't have to prefix schemas with `lakekeeper`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = connect(\n",
    "    host=TRINO_URI,\n",
    "    auth=OAuth2Authentication(REDIRECT_HANDLER),\n",
    "    http_scheme=\"https\",\n",
    "    verify=False,\n",
    "    catalog=\"lakekeeper\" # This line is new\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur = conn.cursor()\n",
    "cur.execute(\"CREATE SCHEMA IF NOT EXISTS finance\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur.execute(\"CREATE TABLE IF NOT EXISTS finance.revenue (year INT, revenue DOUBLE)\")\n",
    "cur.execute(\"INSERT INTO finance.revenue VALUES (2023, 10342.1), (2024, 10645.2)\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Execute query and fetch all rows\n",
    "cur.execute(\"SELECT * FROM finance.revenue\")\n",
    "rows = cur.fetchall()\n",
    "\n",
    "# Get column names\n",
    "columns = [desc[0] for desc in cur.description]\n",
    "\n",
    "# Create DataFrame\n",
    "df = pd.DataFrame(rows, columns=columns)\n",
    "\n",
    "# Display DataFrame\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur.execute(\"CREATE TABLE IF NOT EXISTS finance.products (product_id INT, description VARCHAR)\")\n",
    "cur.execute(\"INSERT INTO finance.products VALUES (1, 'Product 1'), (2, 'Product 2')\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Execute query and fetch all rows\n",
    "cur.execute(\"SELECT * FROM finance.products\")\n",
    "rows = cur.fetchall()\n",
    "\n",
    "# Get column names\n",
    "columns = [desc[0] for desc in cur.description]\n",
    "\n",
    "# Create DataFrame\n",
    "df = pd.DataFrame(rows, columns=columns)\n",
    "\n",
    "# Display DataFrame\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
